package com.sk.weichat.ui.me.wallet;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.sk.weichat.R;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.util.ToastUtil;
import com.sk.weichat.view.PasswordInputEditText;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.Call;

public class ResetPayPwdConfirmActivity extends BaseActivity {
    PasswordInputEditText edtPswd;

    Button btnConfirm;

    private static final String pswd_key = "pswd";
    private String newPswd;

    //输入错2次返回重新输入第一次
    private boolean once;

    //新旧密码相同的返回码
    private int code_pswd = 1040318;

    public static void startResetPayPwdConfirmActivityForResult(String newPswd, int requestCode, Activity context) {
        Intent intent = new Intent(context, ResetPayPwdConfirmActivity.class);
        intent.putExtra(pswd_key, newPswd);
        context.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_pay_pwd_confirm);
        newPswd = getIntent() != null ? getIntent().getStringExtra(pswd_key) : "";
        edtPswd = findViewById(R.id.edt_pswd);
        TextView tvTitle = findViewById(R.id.tv_title_center);
        tvTitle.setText(getString(R.string.reset_pwd_title));
        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnConfirm = findViewById(R.id.btn_confirm);
        btnConfirm.setEnabled(false);
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //确定
                resetPayPswd(edtPswd.getText(), coreManager.getSelfStatus().accessToken);
            }
        });

        edtPswd.setTextWatcher(new Observer<CharSequence>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(CharSequence charSequence) {
               // Log.e("lzs", "charSequence:" + charSequence);
                String pswd = charSequence.toString();
                if (pswd.length() == 6) {
                    if (pswd.equals(newPswd)) {
                        //两次密码相同，
                        btnConfirm.setEnabled(true);
                        btnConfirm.setBackgroundResource(R.drawable.shape_corner_26dp_solid_primary);
                    } else {
                       // Log.e("lzs","xxxx");
                        if (once) {
                            ToastUtil.showToast(mContext, getString(R.string.confirm_two_failed));
                            setResult(Activity.RESULT_CANCELED);
                            finish();
                            return;
                        }
                        once = true;
                        //两次密码不相同
                        btnConfirm.setEnabled(false);
                        ToastUtil.showToast(mContext, getString(R.string.input_pswd_confirm_incorrect));
                        edtPswd.setText("");
                        btnConfirm.setBackgroundResource(R.drawable.shape_corner_26_solid_gray_bdc2c8);
                    }
                } else {
                    btnConfirm.setEnabled(false);
                    btnConfirm.setBackgroundResource(R.drawable.shape_corner_26_solid_gray_bdc2c8);
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
        //弹出键盘输入
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }

    //重置密码
    private void resetPayPswd(String newPswd, String access_token) {
        Map<String, String> params = new HashMap<>();
        params.put("access_token", access_token);
        params.put("newPassword", newPswd);
        params.put("affirmPassword", newPswd);
        HttpUtils.post().url(coreManager.getConfig().RESET_PAY_PASSWORD)
                .params(params)
                .build()
                .execute(new BaseCallback<Void>(Void.class) {

                    @Override
                    public void onResponse(ObjectResult<Void> result) {
                        if (result.getResultCode() == 1) {
                            //修改支付密码成功
                            ToastUtil.showToast(mContext, "重置支付密码成功");
                            setResult(Activity.RESULT_OK);
                            finish();
                        } else if (result.getResultCode() == code_pswd) {
                            //新旧密码相同
                            ToastUtil.showToast(mContext, result.getResultMsg());
                            finish();
                        } else {
                            ToastUtil.showToast(mContext, result.getResultMsg());
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {

                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
